package com.ycx.ycxmybatismanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ycx.ycxmybatismanagement.dto.ClazzsListDTO;
import com.ycx.ycxmybatismanagement.exception.BizException;
import com.ycx.ycxmybatismanagement.mapper.ClazzsMapper;
import com.ycx.ycxmybatismanagement.dto.ClazzDTO;
import com.ycx.ycxmybatismanagement.mapper.EmpMapper;
import com.ycx.ycxmybatismanagement.pojo.Clazz;
import com.ycx.ycxmybatismanagement.pojo.PageBean;
import com.ycx.ycxmybatismanagement.service.ClazzsService;
import com.ycx.ycxmybatismanagement.vo.ClazzsListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClazzsServiceImpl implements ClazzsService {
    @Autowired
    private ClazzsMapper clazzsMapper;


    /**
     * 班级分页
     * @param clazzsListDTO
     * @return
     */
    @Override
    public PageBean<ClazzsListVO> clazzsPageList(ClazzsListDTO clazzsListDTO) {
        PageHelper.startPage(clazzsListDTO.getPage(),clazzsListDTO.getPageSize());

        Page<ClazzsListVO> clazzsListPage = clazzsMapper.clazzsPageList(clazzsListDTO);

        List<ClazzsListVO> clazzsListVOS = clazzsListPage.getResult().stream().map(clazzsListVO -> {
            String status = ClazzsServiceImpl.clazzStatus(clazzsListVO.getBeginDate(), clazzsListVO.getEndDate());
            clazzsListVO.setStatus(status);
            return clazzsListVO;
        }).collect(Collectors.toList());
        return new PageBean(clazzsListPage.getTotal(),clazzsListVOS);
    }

    /**
     * 班级删除
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteClazzById(Integer id) {
        int count =  clazzsMapper.getStudentSize(id);
        if (count==0){
            clazzsMapper.deleteClazzById(id);
        }
        else {
            throw  new BizException("班级有学生,无法删除");
        }
    }

    /**
     * 班级添加
     * @param clazzDTO
     */
    @Override
    public void addClazz(ClazzDTO clazzDTO) {

//        String status = ClazzsServiceImpl.clazzStatus(clazzDTO.getBeginDate(), clazzDTO.getEndDate());
//        clazzDTO.setStatus(status);
        clazzDTO.setCreateTime(LocalDate.now());
        clazzDTO.setUpdateTime(LocalDate.now());
        clazzsMapper.addClazz(clazzDTO);
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @Override
    public ClazzsListVO selectClazzById(Integer id) {
       ClazzsListVO clazz = clazzsMapper.selectClazzById(id);
        return clazz;
    }

    /**
     * 修改
     * @param clazzDTO
     */
    @Override
    public void updateClazz(ClazzDTO clazzDTO) {
        Clazz clazz = new Clazz();
        BeanUtils.copyProperties(clazzDTO,clazz);

//        String name =  clazzsMapper.selectClazzName(clazz.getName());
//        if (name.isEmpty()){
            clazzsMapper.updateClazz(clazz);
//        }else {
//            throw new BizException("重名,换一个名字");
//        }

    }

    /**
     * 查询所有班级
     * @return
     */
    @Override
    public List<ClazzsListVO> selectClazzList() {
        List<ClazzsListVO> clazzsList = clazzsMapper.selectClazzList();
        return clazzsList;
    }


    /**
     * 开班状态
     * @param beginDate
     * @param endDate
     * @return
     */
    public static String clazzStatus(LocalDate beginDate,LocalDate endDate){
        String status = "";
        if (beginDate.isBefore(LocalDate.now()) && LocalDate.now().isAfter(endDate)) {
            status = "在读";
        }
        //    - 开课时间 > 当前时间：未开班
         if (LocalDate.now().isBefore(beginDate) ){
            status = "未开班";
        }
        //    - 结课时间 < 当前时间：已结课
        if (LocalDate.now().isAfter(endDate)) {
            status = "已结课";
        }
       return status;
    }
}
